import 'package:flutter/material.dart';
import 'package:wechat/const.dart';
import 'package:wechat/friend/friend_data.dart';

class IndexBar extends StatefulWidget {
  const IndexBar({super.key});

  @override
  State<IndexBar> createState() => _IndexBarState();
}

class _IndexBarState extends State<IndexBar> {
  Color _bgColor = const Color.fromRGBO(0, 0, 0, 0);
  Color _textColor = Colors.black;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final List<Widget> letterList = [];
    for (int i = 0; i < indexLetterList.length; i++) {
      letterList.add(
        Expanded(
          child: Text(
            indexLetterList[i],
            style: TextStyle(color: _textColor),
          ),
        ),
      );
    }
    return Positioned(
      top: screenHeight(context) / 8,
      right: 4,
      child: SizedBox(
        width: 24,
        height: screenHeight(context) / 2,
        child: Container(
          color: _bgColor,
          child: GestureDetector(
            child: Column(children: letterList),
            onVerticalDragUpdate: (DragUpdateDetails details) {
              RenderBox renderBox = context.findRenderObject() as RenderBox;
              double y = renderBox.globalToLocal(details.globalPosition).dy;
              var itemHeight = screenHeight(context) / 2 / letterList.length;
              var index =
                  (y ~/ itemHeight).clamp(0, indexLetterList.length - 1);
              setState(() {
                _bgColor = const Color.fromRGBO(1, 1, 1, 0.5);
                _textColor = Colors.white;
              });
            },
            onVerticalDragEnd: (DragEndDetails details) {
              setState(() {
                _bgColor = const Color.fromRGBO(0, 0, 0, 0);
                _textColor = Colors.black;
              });
            },
          ),
        ),
      ),
    );
  }
}
